# -*- coding: latin-1 -*-

from PyQt4 import QtCore, QtGui

import Code.TrListas as TrListas

import Code.QT.QTUtil2 as QTUtil2
import Code.QT.Colocacion as Colocacion
import Code.QT.Iconos as Iconos

import Code.QT.Grid as Grid
import Code.QT.Columnas as Columnas
import Code.QT.Delegados as Delegados

# --------------------------------------------------------------------------------------------------------
class WEtiquetasPGN( QtGui.QDialog ):

    def __init__(self, procesador, liPGN )  :

        wParent = procesador.pantalla
        self.procesador = procesador
        self.liPGN = liPGN[:] # Trabajamos con la copia
        while len(self.liPGN) < 30 :
            self.liPGN.append( ["",""] )

        super(WEtiquetasPGN, self).__init__(wParent)

        self.setWindowTitle( _("Edit PGN labels" ) )
        self.setWindowIcon( Iconos.PGN() )
        self.setWindowFlags( QtCore.Qt.Dialog | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowMinimizeButtonHint )

        #~ self.setMinimumWidth( 450 )

        # Toolbar
        tb = QTUtil2.creaTBAceptarCancelar( self )
        # Lista
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "ETIQUETA", _( "Label" ), 150, edicion=Delegados.LineaTextoUTF8() )
        oColumnas.nueva( "VALOR", _( "Value" ), 400, edicion=Delegados.LineaTextoUTF8() )

        self.grid = Grid.Grid( self, oColumnas, siEditable=True )
        n = self.grid.anchoColumnas()
        self.grid.setFixedWidth( n + 20 )

        # Layout
        layout = Colocacion.V().control(tb).control(self.grid).margen( 3 )
        self.setLayout( layout )

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        accion = self.sender().clave
        if accion == "aceptar" :
            self.accept()
        elif accion == "cancelar" :
            self.reject()

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridNumDatos( self, grid ) :
        return len(self.liPGN)

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridPonValor( self, grid, fila, oColumna, valor ) :
        col = 0 if oColumna.clave == "ETIQUETA" else 1
        self.liPGN[fila][col] = valor

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDato( self, grid, fila, oColumna ) :
        if oColumna.clave == "ETIQUETA" :
            lb = self.liPGN[fila][0]
            ctra = lb.upper()
            trad = TrListas.pgnLabel( lb )
            if trad != ctra :
                clave = trad
            else :
                if lb :
                    clave = lb#[0].upper()+lb[1:].lower()
                else :
                    clave = ""
            return clave
        return self.liPGN[fila][1]

# --------------------------------------------------------------------------------------------------------
def editarEtiquetasPGN( procesador, liPGN ) :
    w = WEtiquetasPGN( procesador, liPGN )
    if w.exec_() :
        li = []
        for eti, valor in w.liPGN :
            if (len(eti.strip()) > 0)  and (len(valor.strip()) > 0) :
                li.append( [eti,valor] )
        return li
    else :
        return None
# --------------------------------------------------------------------------------------------------------

